package es.cazadoresmadridejos.beans.dao.temps.impl;

import java.sql.SQLException;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import es.cazadoresmadridejos.beans.dao.temps.TempsDao;
import es.cazadoresmadridejos.common.bean.dao.impl.DaoImpl;
import es.cazadoresmadridejos.model.Temps;

@Repository("tempsDao")
public class TempsDaoImpl extends DaoImpl<Temps> implements TempsDao{

	@Override
	public Temps newInstance() {
		return new Temps();
	}

	@Override
	public Integer getNumeric(final String key) {
		return (Integer) getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {

				StringBuilder hql = new StringBuilder();
				hql
					.append("select r.numeric ");
				hql.append(" from Temps r ")
					.append(" where r.key = :key ")
					.append("");
				
				Query query = session.createQuery(hql.toString());
				
				query.setString("key", key);

				return query.uniqueResult();
			}
		});
		
	}


	@Override
	public String getValue(final String key) {
		return (String) getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {

				StringBuilder hql = new StringBuilder();
				hql
					.append("select r.vaue ");
				hql.append(" from Temps r ")
					.append(" where r.key = :key ")
					.append("");
				
				Query query = session.createQuery(hql.toString());
				
				query.setString("key", key);

				return query.uniqueResult();
			}
		});
		
	}

	@Override
	public Temps getByKey(final String key) {
		return (Temps) getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {

				StringBuilder hql = new StringBuilder();
				hql.append("select r ").append(" from Temps r " ).append("where r.key = :key ").append("");

				Query query = session.createQuery(hql.toString());
				
				query.setString("key", key);

				return query.uniqueResult();
			}
		});
	}


}
